<?php
/*
Plugin Name: LiveChat-widget
Plugin URI: http://www.atop-online.com
Description: Live Chat Widget
Author: Selver.D && Evan.chen
Version: 0.0.1
Author URI: http://www.atop-online.com
*/

/* initializing */
function live_chat_init()
{
	if ( !function_exists("register_sidebar_widget") )
	return;
	register_sidebar_widget(__('Live Chat Message', 'livechat'), "live_chat");
	register_widget_control(__('Live Chat Message', 'livechat'), "live_chat_control");
}

function live_chat_control()
{
	// Configuration panel for Time Machine Widget
	$options = get_option("live_chat");
	
	if ( !is_array( $options ) )
	{
		$options = array(
			"title"           => __('Recent Chat Messages', 'livechat'),
			"posts"           => "5",
			"message"         => __('No Message', 'livechat'),
		);
		update_option("live_chat", $options);
	}

	if ( $_POST['chat-Submit'] )
	{
		$options['title']   = htmlspecialchars($_POST['chat-wTitle']);
		$options['message'] = htmlspecialchars($_POST['chat-wMessage']);
		$options['posts']   = htmlspecialchars($_POST['chat-wPosts']);
		update_option("live_chat", $options);
	}

	?>
	<p>
	<label for="chat-wTitle"><?php _e('Title', 'tm'); ?>: </label><br />
	<input type="text" id="chat-wTitle" name="chat-wTitle" value="<?php echo $options['title'];?>" /><br /><br />
	<label for="chat-wPosts"><?php _e('Number of posts', 'tm'); ?>: </label><br />
	<input type="text" id="chat-wPosts" name="chat-wPosts" value="<?php echo $options['posts']; ?>" /><br />
	<em><?php _e('Default value is', 'tm'); ?> <strong>5</strong></em><br />
	<label for="chat-wMessage"><?php _e('Message when no posts in past', 'tm'); ?></label><br />
	<input type="text" id="chat-wMessage" name="chat-wMessage" value="<?php echo $options['message']; ?>" /><br /><br />
	<input type="hidden" id="chat-Submit" name="chat-Submit" value="1" />
	</p>
<?php
} // function tm_widget_control

function live_chat($args) {
	$perma = get_settings('permalink_structure');
	// get globals
	global $wpdb;
	$blog_url = get_bloginfo('wpurl');
	// get widget options (title and number of post to list)
	$options = get_option("live_chat");
	// setup SQL query
	$sql="Select * from `wp_chat` where `receiver_id`=0 order by `timestamp` desc limit 0,".$options['posts'];
	$recent_message=$wpdb->get_results($sql);
	
	
	$page_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status='publish' AND post_name='live-chat'");
	// extract WP args and print beginning of widget
	extract($args);
	echo "$before_widget $before_title ".$options['title']." $after_title<ul>";

	// execute SQL query and track error
	$recent_message = $wpdb->get_results($sql);
	// check if we got some posts
	if ( $recent_message ) {
		// if we got posts, parse it
		foreach ( $recent_message as $post )
		{
			// print formated post line
			if (!$perma) {
				echo "<li><a href='$blog_url/?page_id=$page_id#m_" . $post->id ."'>".substr($post->text,0,50);
				if(strlen($post->text) >=50) 
				{
					echo "..."; 
				} 
				echo "</a></li>";
			} else {
                echo "<li><a href='$blog_url/live-chat/#m_" . $post->id ."'>".substr($post->text,0,50);
				if(strlen($post->text) >=50) 
				{
					echo "..."; 
				} 
				echo "</a></li>";
            }
		}
	} else {
		// if we have not blog posts on current date in past, print that info
		echo "<li>".$options['message']."</li>";
	}
	echo "</ul>$after_widget";
}

// Check for uninstall hook
if ( function_exists('register_uninstall_hook') )
	register_uninstall_hook(__FILE__, 'live_chat_uninstall');

// Delete options from database
function live_chat_uninstall()
{
	delete_option("live_chat");
}

// initialise Time Machin plugin
add_action("widgets_init", "live_chat_init");
?>